//  program:	Replication of models for JPIPE Published version
//  task:       *replicated models in tables
//				project: Electoral Costs

//  author:     Josh Ryan \ July 20, 2022

// #Start
// program setup

set more off



use "predictpartyunitywnom_stata_v4_021721.dta", clear

*codebook
describe

xtset panelid cong

*Table: District-Assignment Estimates for ideology, 104th-114th
xtreg mem_wnomsd dispart i.cong i.stewartcommid, fe cluster(panelid)
xtreg mem_wnomsd dispart i.commcong, fe cluster(panelid)
xtreg absmem absdispart memberparty i.cong i.stewartcommid, fe cluster(panelid)
xtreg absmem absdispart memberparty i.commcong, fe cluster(panelid)


*member fixed effects--generate differenced variables and set units
xtset icpsrid cong

*Table: Legislator-Assignment Estimates for ideology, 104th-114th
xtreg mem_wnomsd dispart i.cong i.stewartcommid, fe cluster(icpsrid)
xtreg mem_wnomsd dispart i.commcong, fe cluster(icpsrid)

xtreg absmem absdispart memberparty i.cong i.stewartcommid, fe cluster(icpsrid)
xtreg absmem absdispart memberparty i.commcong, fe cluster(icpsrid)



*Table: First Differences Estimates for Ideology, 104-114th

*generate differenced variables

xtset panelid cong

gen ldispart=L.dispart
gen ddispart=d.dispart
gen dnom=d.mem_wnomsd
gen dabsmem=d.absmem
gen dabsdispart=d.absdispart

reg dnom ddispart i.cong i.stewartcommid, cluster(panelid)
reg dnom ddispart i.commcong, cluster(panelid)
reg dnom c.ddispart##c.numterms i.cong i.stewartcommid, cluster(panelid)
margins, dydx(ddispart) at(numterms=(0(1)30)) post vsquish 

reg dabsmem dabsdispart memberparty i.cong i.stewartcommid, cluster(panelid)
reg dabsmem dabsdispart memberparty i.commcong, cluster(panelid)
reg dabsmem c.dabsdispart##c.numterms memberparty i.cong i.stewartcommid, cluster(panelid)








*Table: Predicting OC z-scores after redistricting

*load redistricting data from natural experiment process
use "nomexperiment_v1_stata_010321.dta", clear

*generate absolute values of some variables and scale properly
gen absdispart_post=abs(dispart_post)
gen absdispart_diff=abs(dispart_diff)
gen absmem_wnomsd_diff=abs(mem_wnomsd_diff)
replace dispart_post=dispart_post/100
replace absdispart_post=absdispart_post/100
replace absdispart_diff=absdispart_diff/100
gen cyclecomm=redistcycle*stewartcommid
replace memberparty=(memberparty/100)-1

*models shown in table
reg mem_wnomsd_post dispart_post i.redistcycle i.stewartcommid, cluster(panelid)
reg mem_wnomsd_post dispart_post i.cyclecomm, cluster(panelid)
reg absmem absdispart_post memberparty i.redistcycle i.stewartcommid, cluster(panelid)
reg absmem absdispart_post memberparty i.cyclecomm, cluster(panelid)

reg mem_wnomsd_diff dispart_diff i.redistcycle i.stewartcommid, cluster(panelid)
reg mem_wnomsd_diff dispart_diff i.cyclecomm, cluster(panelid)
reg absmem_wnomsd_diff absdispart_diff memberparty i.redistcycle i.stewartcommid, cluster(panelid)
reg absmem_wnomsd_diff absdispart_diff memberparty i.cyclecomm, cluster(panelid)

*directional effects, not shown but referenced in paper
reg mem_wnomsd dispart_post memberparty i.redistcycle i.stewartcommid, cluster(panelid)
reg mem_wnomsd dispart_post memberparty i.cyclecomm, cluster(panelid)

reg mem_wnomsd_diff dispart_diff memberparty i.redistcycle i.stewartcommid, cluster(panelid)
reg mem_wnomsd_diff dispart_diff memberparty i.cyclecomm, cluster(panelid)




*District-Assignment Estimates of the Conditional Effect of Partisan- and Information-Induced Preferences on Ideology

*load panel data
use "predictpartyunitywnom_stata_v4_021721.dta", clear

*set panels
xtset panelid cong

*Table: District Assignment Two-Way Fixed Effects Estimates of the conditional effect of partisan and information induced preferences on ideology 
*Table shown in appendix C1, marginal effects graphs shown in main text
xtreg mem_wnomsd c.dispart##c.permaj##c.stafflog i.cong i.stewartcommid, fe cluster(panelid)
xtreg absmem c.absdispart##c.permaj##c.stafflog memberparty i.cong i.stewartcommid, fe cluster(panelid)

*Figure: Marginal Effect of District Partisanship Conditional on Percentage of Committee Seats Controlled by Majority Party and Committee Staff
use "predictpartyunitywnom_stata_v4_021721.dta", clear

xtset panelid cong

sum permaj
sum stafflog

*same model as above just finding marginal effects--left panel
xtreg mem_wnomsd c.dispart##c.permaj##c.stafflog i.cong i.stewartcommid, fe cluster(panelid)
margins, dydx(dispart) at(permaj=(.52(.01).73) stafflog==5.05) post vsquish 
test 1._at  == 22._at

*right panel
xtreg mem_wnomsd c.dispart##c.permaj##c.stafflog i.cong i.stewartcommid, fe cluster(panelid)
margins, dydx(dispart) at(stafflog=(3.25(.1)5.05) permaj==.727)  post vsquish 
test 1._at  == 19._at

*See R file for creating figures: 03_figures_v1_050922


*Figure: Marginal Effect of Absolute District Partisanship Conditional on Percentage of Committee Seats Controlled by Majority Party and Committee Staff
use "predictpartyunitywnom_stata_v4_021721.dta", clear

xtset panelid cong

sum permaj
sum stafflog

xtreg absmem c.absdispart##c.permaj##c.stafflog i.cong i.stewartcommid, fe cluster(panelid)
margins, dydx(absdispart) at(permaj=(.52(.01).73) stafflog==5.05) post vsquish 
test 1._at  == 22._at

*See R file: 03_figures_v1_050922


*Table: District-Assignment Two-way fixed effects Estimates of the Conditional Effect of Vote Share on Ideology
*Table shown in appendix C@, marginal effects graphs shown in main text

use "predictpartyunitywnom_stata_v4_021721.dta", clear

xtset panelid cong

replace voteshare=voteshare/100

xtreg mem_wnomsd c.dispart##c.voteshare c.permaj c.stafflog i.cong i.stewartcommid, fe cluster(panelid)
margins, dydx(dispart) at(voteshare=(50(5)100)) post vsquish 
test 1._at  = 11._at

*Right Panel, Abs. Dist. Dem. Support
xtreg absmem c.absdispart##c.voteshare permaj c.stafflog memberparty i.cong i.stewartcommid, fe cluster(panelid)
margins, dydx(absdispart) at(voteshare=(50(5)100)) post vsquish 
test 1._at  == 11._at

*See R file: 03_figures_v1_050922

*Appendix A in Descriptive R files

*Appendix B - Party Unity

use "predictpartyunitywnom_stata_v4_021721.dta", clear

xtset panelid cong

*generate differenced variables
gen ldispart=L.dispart
gen ddispart=d.dispart
gen dnom=d.mem_wnomsd
gen dabsmem=d.absmem
gen dabsdispart=d.absdispart
gen dunity=d.unity

*Table: District Two-Way Fixed Effects Estimates for Party Unity, 104th-114th Congresses

xtreg unity c.absdispart memberparty i.cong i.stewartcommid, fe cluster(panelid)
xtreg unity c.absdispart memberparty i.commcong, fe cluster(panelid)
xtreg dunity c.dabsdispart memberparty i.cong i.stewartcommid, fe cluster(panelid)

use "predictpartyunitywnom_stata_v4_021721.dta", clear

*member fixed effects--
xtset icpsrid cong

*generate differenced variables
gen ldispart=L.dispart
gen ddispart=d.dispart
gen dnom=d.mem_wnomsd
gen dabsmem=d.absmem
gen dabsdispart=d.absdispart
gen dunity=d.unity

xtreg unity c.absdispart memberparty i.cong i.stewartcommid, fe cluster(icpsr)
xtreg unity c.absdispart memberparty i.commcong, fe cluster(icpsr)
xtreg dunity c.dabsdispart memberparty i.cong i.stewartcommid, fe cluster(icpsr)


*Appendix C - Shown Above

*Appendix D - Results Split by Constituent and Non-Constituent Committees

use "predictpartyunitywnom_stata_v4_021721.dta", clear

describe

*keep constituent Committees


*set units
xtset panelid cong

*generate differenced variables
gen ldispart=L.dispart
gen ddispart=d.dispart
gen dnom=d.mem_wnomsd
gen dabsmem=d.absmem
gen dabsdispart=d.absdispart

keep if stewartcommid==102 | stewartcommid==104 | stewartcommid==106 | stewartcommid==164 | stewartcommid==173 | stewartcommid==182 | stewartcommid==184


*Table: District-Assignment Estimates for ideology--Constituent Committees, 104th-114th
xtreg mem_wnomsd dispart i.cong i.stewartcommid, fe cluster(panelid)
xtreg mem_wnomsd dispart i.commcong, fe cluster(panelid)
reg dnom ddispart i.cong i.stewartcommid, cluster(panelid)

xtreg absmem absdispart memberparty i.cong i.stewartcommid, fe cluster(panelid)
xtreg absmem absdispart memberparty i.commcong, fe cluster(panelid)
reg dabsmem dabsdispart memberparty i.cong i.stewartcommid, cluster(panelid)


use "predictpartyunitywnom_stata_v4_021721.dta", clear

describe

*keep constituent Committees


*set units
xtset panelid cong

*generate differenced variables
gen ldispart=L.dispart
gen ddispart=d.dispart
gen dnom=d.mem_wnomsd
gen dabsmem=d.absmem
gen dabsdispart=d.absdispart

drop if stewartcommid==102 | stewartcommid==104 | stewartcommid==106 | stewartcommid==164 | stewartcommid==173 | stewartcommid==182 | stewartcommid==184

*Table: District-Assignment Estimates for ideology--Non-Constitution Committees, 104th-114th
xtreg mem_wnomsd dispart i.cong i.stewartcommid, fe cluster(panelid)
xtreg mem_wnomsd dispart i.commcong, fe cluster(panelid)
reg dnom ddispart i.cong i.stewartcommid, cluster(panelid)

xtreg absmem absdispart memberparty i.cong i.stewartcommid, fe cluster(panelid)
xtreg absmem absdispart memberparty i.commcong, fe cluster(panelid)
reg dabsmem dabsdispart memberparty i.cong i.stewartcommid, cluster(panelid)





*Appendix E - Main Results Using Groseclose-Levitt-Snyder Adjusted Conservative Vote Probabilities and OC Scores

use "predictpartyunitywnom_stata_v4_021721.dta", clear

describe

*set units
xtset panelid cong

*generate differenced variables
gen ldispart=L.dispart
gen ddispart=d.dispart
gen dcvp=d.cvp_adjusted
gen abscvp=abs(cvp_adjusted)
gen dabscvp=d.abscvp
gen dabsdispart=d.absdispart

*Table: District-Assignment Estimates for Adjusted Conservative Vote Probabilities, 104th-114th Congressesxtreg cvp_adjusted dispart i.cong i.stewartcommid, fe cluster(panelid)
xtreg cvp_adjusted dispart i.cong i.stewartcommid, fe cluster(panelid)
xtreg cvp_adjusted dispart i.commcong, fe cluster(panelid)
reg dcvp ddispart i.cong i.stewartcommid, cluster(panelid)

xtreg abscvp absdispart memberparty i.cong i.stewartcommid, fe cluster(panelid)
xtreg abscvp absdispart memberparty i.commcong, fe cluster(panelid)
reg dabscvp dabsdispart memberparty i.cong i.stewartcommid, cluster(panelid)


use "predictpartyunitywnom_stata_v4_021721.dta", clear

describe

*set units
xtset panelid cong

*generate differenced variables
gen ldispart=L.dispart
gen ddispart=d.dispart
gen docadj=d.wnom_adjusted
gen absocadj=abs(wnom_adjusted)
gen dabsocadj=d.absocadj
gen dabsdispart=d.absdispart

*Table: District-Assignment Estimates for Adjusted OC Scores, 104th-114th Congressesxtreg cvp_adjusted dispart i.cong i.stewartcommid, fe cluster(panelid)
xtreg wnom_adjusted dispart i.cong i.stewartcommid, fe cluster(panelid)
xtreg wnom_adjusted dispart i.commcong, fe cluster(panelid)
reg docadj ddispart i.cong i.stewartcommid, cluster(panelid)

xtreg absocadj absdispart memberparty i.cong i.stewartcommid, fe cluster(panelid)
xtreg absocadj absdispart memberparty i.commcong, fe cluster(panelid)
reg dabsocadj dabsdispart memberparty i.cong i.stewartcommid, cluster(panelid)

